<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>菜单列表管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="${global_templets_skin}/systemstyle/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="${global_templets_skin}/systemstyle/style/admin.css" media="all">
  <link rel="stylesheet" href="${global_templets_skin}/systemstyle/layui/treetable-lay/treetable.css" media="all">
  <style>
    .search{
      height: 36px;
      line-height: 36px;
      padding-left: 36px;
    }
  </style>
</head>
<body layadmin-themealias="default">
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>组件</cite></a>
        <a><cite>数据表格</cite></a>
        <a><cite>数据表格的重载</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding-top: 10px; padding-bottom: 10px;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">菜单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="poster" placeholder="请输入菜单名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">菜单类型</label>
                    <div class="layui-input-block">
                        <select name="visible">
                            <option value="">所有</option>
                            <option value="0">显示</option>
                            <option value="1">隐藏</option>
                        </select>
                        <div class="layui-unselect layui-form-select"><div class="layui-select-title"><input type="text" placeholder="请选择" value="正常" readonly="" class="layui-input layui-unselect"><i class="layui-edge"></i></div><dl class="layui-anim layui-anim-upbit"><dd lay-value="0" class="layui-this">正常</dd><dd lay-value="1" class="">置顶</dd><dd lay-value="2" class="">封禁</dd></dl></div>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-forum-list search" lay-submit="" lay-filter="LAY-app-forumlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>


        <div class="layui-card-body">
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-group">
                    <button class="layui-btn layui-btn-sm" lay-event="add_lay"><i class="layui-icon">&#xe654;</i>新增</button>
                    <button class="layui-btn layui-btn-sm" lay-event="updata_lay"><i class="layui-icon">&#xe642;</i>修改</button>
                    <button class="layui-btn layui-btn-sm" lay-event="delete_lay"><i class="layui-icon">&#xe640;</i>删除</button>
                    <button class="layui-btn layui-btn-sm" lay-event="refresh_lay"><i class="layui-icon">&#xe666;</i>刷新</button>
                </div>
            </script>
            <table class="layui-hide" id = "menu" lay-filter="menu"></table>

            <script type="text/html" id="menu-table-switchTpl">
                <!-- 这里的 checked 的状态只是演示 -->
                <input type="checkbox" name="visible" lay-skin="switch" lay-text="显示|隐藏" lay-filter="menu-table-visible"
                       value="{{ d.visible }}" data-id="{{d.id}}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.visible == false ? '' : 'checked' }}>
            </script>

            <script type="text/html" id="menuBar">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs" lay-event="add">新增</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
            <blockquote class="layui-elem-quote">此处是网站后台左侧菜单管理。</blockquote>

        </div>
    </div>
</div>
  <script src="${global_templets_skin}/systemstyle/layui/layui.js"></script>
  <script>
  layui.config({
      base : '${global_templets_skin}/systemstyle/'
  }).extend({
      index: 'lib/index', //主入口模块
      treetable : '../layui/treetable-lay/treetable'
  }).use(['index','treetable', 'table', 'layer'], function () {
      var table = layui.table
          ,layer = layui.layer
          ,form = layui.form
          ,$ = layui.$
          ,treetable = layui.treetable;
      //渲染表格
      var renderTable = function(){
          layer.load(2);
          treetable.render({
              id:'menu',
              treeColIndex: 1,	//树形图标显示在第几列
              treeSpid: '0',		//最上级的父级id
              treeIdName: 'id',	//id字段的名称
              treePidName: 'parentId',	//pid字段的名称，父级菜单id
              treeDefaultClose: true,	//是否默认折叠
              treeLinkage: false,		//父级展开时是否自动展开所有子级
              elem: '#menu',	//表格id
              url: '/admin/menu/admin_listData.do',
              toolbar: '#toolbarDemo',
              page: false,
              cellMinWidth: 120,
              cols: [[
                  {type:'checkbox'},
                  {field: 'menuName', title: '名称', width:180},
                  {field: 'url' , title: '地址', width:180},
                  {field: 'perms', title: '权限', width:200},
                  {field: 'menuType', title: '类型',align: 'center', width:150,
                      templet: function(d){
                          if(d.menuType=="M"){
                              return '目录';
                          }else if(d.menuType=="C"){
                              return '菜单';
                          }else if(d.menuType=="F"){
                              return '按钮';
                          }
                      }
                  },
                  {field:'visible', title:'显示',align: 'center', width:125, templet: '#menu-table-switchTpl', unresize: true},
                  {field: 'sortOrder', title: '排序',align: 'center', width:120},
                  {fixed: 'right',align: 'center', title:'操作', toolbar: '#menuBar'}
              ]],
              //数据渲染完的回调
              done: function () {
                  //关闭加载
                  layer.closeAll('loading');
              }
          })
      };
      renderTable();

      //监听显示操作
      form.on('switch(menu-table-visible)', function(obj){
          //var json = JSON.parse(decodeURIComponent($(this).data('json')));
          //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
          var checked = obj.elem.checked;
          var id = $(this).attr("data-id");
          $.ajax({
              url:'/admin/menu/visible_admin_menu.do',
              type:'post',
              dataType: 'json',
              data: {id:id,visible:checked},
              success:function(e){
                  if (e.code == "0") {
                      if(e.data.visible=="true"){
                          obj.elem.checked = !checked;
                          form.render();
                      }else{
                          obj.elem.checked = checked;
                          form.render();
                      }
                  }else {
                      layer.msg(e.message);
                      return;
                  }
              }
          })
      });

      table.on('toolbar', function(obj){
          var checkStatus = table.checkStatus('menu');
          var data = checkStatus.data;
          if(obj.event === 'add_lay'){
              if(data.length > 1){
                  layer.msg("请选择一行作为父级菜单");
              }else{
                  var id = data.length==1?data[0].id: '';
                  //$.get('/admin/menu/add_admin_menu.do', {parentId: id}, function(data){
                      layer.open({
                          type: 2
                          ,title: '添加角色'
                          ,content: "/admin/menu/add_admin_menu.do?id="+id
                          ,maxmin: true
                          ,area: ['500px', '450px']
                          ,btn: ['确定', '取消']
                          ,yes: function(index, layero){
                              var iframeWindow = window['layui-layer-iframe'+ index]
                                  ,submitID = 'LAY-user-front-submit'
                                  ,submit = layero.find('iframe').contents().find('#'+ submitID);

                              //监听提交
                              iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                  //提交 Ajax 成功后，静态更新表格中的数据
                                  $.ajax({
                                      url:'/admin/menu/add_admin_menu.do',
                                      type:'post',
                                      dataType: 'json',
                                      data: data.field,
                                      success:function(e){
                                          if (e.code == "0") {
                                              layer.msg('成功');
                                              renderTable();
                                          }else {
                                              layer.msg(e.message);
                                              return;
                                          }
                                      }
                                  })
                                  layer.close(index); //关闭弹层
                              });

                              submit.trigger('click');
                          }
                      });
                 // })
              }
          }else if(obj.event === "updata_lay"){
              if(data.length != 1){
                  layer.msg("请选择一行进行编辑");
              }else{
                  var id = data[0].id;
                  layer.open({
                      type: 2
                      ,title: '修改菜单'
                      ,content: '/admin/menu/update_admin_menu.do?id='+data.id
                      ,maxmin: true
                      ,area: ['500px', '450px']
                      ,btn: ['确定', '取消']
                      ,yes: function(index, layero){
                          var iframeWindow = window['layui-layer-iframe'+ index]
                              ,submitID = 'LAY-user-front-submit'
                              ,submit = layero.find('iframe').contents().find('#'+ submitID);

                          //监听提交
                          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                              //提交 Ajax 成功后，静态更新表格中的数据
                              $.ajax({
                                  url:'/admin/menu/update_admin_menu.do',
                                  type:'post',
                                  dataType: 'json',
                                  data: data.field,
                                  success:function(e){
                                      if (e.code == "0") {
                                          layer.msg('成功');
                                          renderTable();
                                      }else {
                                          layer.msg(e.message);
                                          return;
                                      }
                                  }
                              })
                              layer.close(index); //关闭弹层
                          });
                          submit.trigger('click');
                      }
                  });
              }
          }else if(obj.event === "delete_lay"){
              if(data.length != 1){
                  layer.msg("请选择一行进行删除！");
              }else{
                  var id = data[0].id;
                  layer.confirm('真的删除选定行的数据吗?', function(index){
                      $.post('/admin/menu/del_admin_menu.do',{id:id},function(result){
                          if(result.success){
                              layer.close(index);
                              renderTable();
                          }
                          layer.msg(result.msg,{offset:'rb'});
                      });
                  });
              }
          }else if(obj.event === "refresh_lay"){
              renderTable();
          }
      })

      //监听行工具事件
      table.on('tool(menu)', function(obj){
          var data = obj.data;
          if(obj.event === 'add'){
              layer.open({
                  type: 2
                  ,title: '添加角色'
                  ,content: "/admin/menu/add_admin_menu.do?parentId="+data.id
                  ,maxmin: true
                  ,area: ['500px', '450px']
                  ,btn: ['确定', '取消']
                  ,yes: function(index, layero){
                      var iframeWindow = window['layui-layer-iframe'+ index]
                          ,submitID = 'LAY-user-front-submit'
                          ,submit = layero.find('iframe').contents().find('#'+ submitID);

                      //监听提交
                      iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                          //提交 Ajax 成功后，静态更新表格中的数据
                          $.ajax({
                              url:'/admin/menu/add_admin_menu.do',
                              type:'post',
                              dataType: 'json',
                              data: data.field,
                              success:function(e){
                                  if (e.code == "0") {
                                      layer.msg('成功');
                                      renderTable();
                                  }else {
                                      layer.msg(e.message);
                                      return;
                                  }
                              }
                          })
                          layer.close(index); //关闭弹层
                      });

                      submit.trigger('click');
                  }
              });
          } else if(obj.event === 'del'){
              layer.confirm('真的删除当前菜单吗？', {icon: 3, type: 0}, function(index){
                  $.ajax({
                      url:'/admin/menu/del_admin_menu.do',
                      type:'post',
                      dataType: 'json',
                      data: {id: data.id},
                      success:function(e){
                          if (e.code == "0") {
                              layer.msg('成功',{icon:1,time:3000});
                              renderTable();
                              return true;
                          }else {
                              layer.msg(e.message,{icon:2, time:3000});
                              return;
                          }
                      }
                  })
                  obj.del();
                  layer.close(index);
              });
          } else if(obj.event === 'edit'){
              var that = this;
              //多窗口模式，层叠置顶
              layer.open({
                  type: 2 //此处以iframe举例
                  ,title: '修改菜单'
                  ,area: ['500px', '450px']
                  ,shade: 0.3
                  ,maxmin: true
/*                  ,offset: [ //为了演示，随机坐标
                      Math.random()*($(window).height()-300)
                      ,Math.random()*($(window).width()-390)
                  ]*/
                  ,content: '/admin/menu/update_admin_menu.do?id='+data.id
                  ,btn: ['确定', '取消']
                  ,yes: function(index, layero){
                      var iframeWindow = window['layui-layer-iframe'+ index]
                          ,submitID = 'LAY-user-front-submit'
                          ,submit = layero.find('iframe').contents().find('#'+ submitID);

                      //监听提交
                      iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                          //提交 Ajax 成功后，静态更新表格中的数据
                          $.ajax({
                              url:'/admin/menu/update_admin_menu.do',
                              type:'post',
                              dataType: 'json',
                              data: data.field,
                              success:function(e){
                                  if (e.code == "0") {
                                      layer.msg('成功');
                                      renderTable();
                                  }else {
                                      layer.msg(e.message);
                                      return;
                                  }
                              }
                          })
                          layer.close(index); //关闭弹层
                      });
                      submit.trigger('click');
                  }
                  ,btn2: function(){
                      layer.closeAll();
                  }
                  ,zIndex: layer.zIndex //重点1
              });
          }
      });


  })
  </script>
</body>
</html>